package name.marz.to.go.ahg.sonybank.executer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import name.marz.to.go.ahg.common.util.HttpUtil;
import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.Parameters;
import name.marz.to.go.ahg.core.exception.UserProcessException;
import name.marz.to.go.ahg.core.executer.AbstractPostExecuter;
import name.marz.to.go.ahg.core.executer.Executer;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.jsoup.nodes.Document;

public class EntryClientBatchExecuter implements Executer<Document> {
	private AutoGetterHttpClient client;
//	private HttpContext context;
	/**
	 * 
	 */

	private final Logger log = Logger.getLogger(EntryClientBatchExecuter.class.getName());
	
	private Document doc;


	public EntryClientBatchExecuter(AutoGetterHttpClient client,
			HttpContext context, Document doc) {
		super();
		this.client = client;
//		this.context = context;
		this.doc = doc;
	}


	@Override
	public Document execute() throws ClientProtocolException, IOException,
			UserProcessException {
		log.fine("cookie store"+this.client.getCookieStore());
		// 暗証番号入力
		log.fine("暗証番号入力開始");
		log.fine("input params "+HttpUtil.getParametersFromInputTags(doc));
		final Parameters parameters = HttpUtil.getParametersFromInputTags(doc);
		AbstractPostExecuter<Parameters> pe = new AbstractPostExecuter<Parameters>(
				HttpUtil.createURISafety("https://o2o.moneykit.net/TDGate/gate/NBW010003")
				, this.client
				, "暗証番号入力") {
			@Override
			protected List<NameValuePair> getRequestParameters() {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				
				params.add(new BasicNameValuePair("B_ID", "2"));
				params.add(new BasicNameValuePair("__type", "0023"));
				params.add(new BasicNameValuePair("Dai2Pin", ""));
				
				params.add(parameters.getNameValuePair("LoginKS"));
				params.add(parameters.getNameValuePair("ZenkaiPCMeisho"));
				params.add(parameters.getNameValuePair("__fid"));
				params.add(parameters.getNameValuePair("KyakuName"));
				params.add(parameters.getNameValuePair("__sid"));
				params.add(parameters.getNameValuePair("CookieErrorSaiLogin"));
				params.add(parameters.getNameValuePair("ZenkaiEndSts"));
				params.add(parameters.getNameValuePair("__gid"));
				params.add(parameters.getNameValuePair("__uid"));
				
				return params;
			}

			@Override
			protected ResponseHandler<Parameters> getResponseHandler() {
				// TODO Auto-generated method stub
				return null;
			}
		};
		final Parameters q1_p = pe.execute();
		log.fine("暗証番号入力終了");
		log.fine("return params"+q1_p);
		
		// 質問に答える１・合言葉
		log.fine("質問に答える１・合言葉 開始");
		pe = new AbstractPostExecuter<Parameters>(
				HttpUtil.createURISafety("https://o2o.moneykit.net/TDGate/gate/NBW010003")
				, this.client
				, "質問に答える１・合言葉入力") {
			@Override
			protected List<NameValuePair> getRequestParameters() {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				String answer1 = "";
				params.add(new BasicNameValuePair("kariRandomAnswer1", ""));
				params.add(new BasicNameValuePair("RandomAnswer1", answer1));
				params.add(new BasicNameValuePair("B_ID", "3"));
				params.add(new BasicNameValuePair("__type", "0023"));
				
				Parameters parameters = q1_p;
				
				params.add(parameters.getNameValuePair("LoginKS"));
				params.add(parameters.getNameValuePair("__gid"));
				params.add(parameters.getNameValuePair("__sid"));
				params.add(parameters.getNameValuePair("__uid"));
				params.add(parameters.getNameValuePair("__fid"));
				
				params.add(parameters.getNameValuePair("ZenkaiPCMeisho"));
				params.add(parameters.getNameValuePair("ZenkaiEndSts"));
				params.add(parameters.getNameValuePair("KyakuName"));
				params.add(parameters.getNameValuePair("CookieErrorSaiLogin"));
				
				return params;
			}

			@Override
			protected ResponseHandler<Parameters> getResponseHandler() {
				// TODO Auto-generated method stub
				return null;
			}
		};
		Parameters q2_p = pe.execute();
		log.fine("質問に答える１・合言葉 終了");
		log.fine("return params"+q2_p);
		
		
		// 質問に答える２・合言葉２
		
		
		// ご利用パソコンの登録
		
		return null;
	}
	
}